// index.js
// 获取应用实例
Page({
  data: {
    loginImage:'https://img1.baidu.com/it/u=3303981320,1355171730&fm=26&fmt=auto',
    userInfo:'',
    username:'',
    userpassword:'',
    userpassword2:'',
    useadmian:''
 },

  onLoad() {

  },
  getUserProfile(e) {
    var that = this;
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        that.setData({
          userInfo: res.userInfo,
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  usernames(e){
    this.setData({
      username : e.detail.value
    })
  },
  getname(e){
   this.setData({
     useadmian : e.detail.value
   })
  },
  getpassword(e){
    this.setData({
      userpassword : e.detail.value
    })},
  getpassword2(e){
    this.setData({
      userpassword2 : e.detail.value
    })
  },
  login(){
    if(this.data.userpassword != this.data.userpassword2){
      wx.showToast({
        title: '密码不相同',
      })
      return
    }
    console.log(Date.now());
    let that = this;
    wx.cloud.database().collection('chat_user').add({
      data:{
        num:Date.now(), //账号
        faceImg: that.data.userInfo.avatarUrl || that.data.loginImage,
        nickName: that.data.userInfo.nickName ||that.data.username,
        admin:that.data.useadmian,
        password:that.data.userpassword 
      }
    }).then(res=>{
      console.log(res);
      // app.globalData.userInfo
      wx.showToast({
          title: '注册成功',
        })
        wx.navigateTo({
          url: '/pages/login/login',
        })
    })
  }
})
